package org.easy.excel.support.config.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.write.handler.WriteHandler;
import org.easy.excel.support.config.AnnotationExcelConfig;
import org.easy.excel.support.handler.AnnotationColumnWidthWriteHandler;
import org.easy.excel.support.handler.AnnotationHeadRowWriteHandler;
import org.easy.excel.support.parse.AnnotationExcelSourceParse;
import org.easy.excel.support.parse.source.ExportLabelSource;
import org.tool.TipException;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 简单的 {@link AnnotationExcelConfig} 实现类
 *
 * @author 李坤
 * @date 2022/6/9 11:03
 */
public class SimpleAnnotationExcelConfig extends AbstractAnnotationExcelConfig {

    /**
     * {@link SimpleAnnotationExcelConfig} 的唯一构造函数
     * <p>
     * 注：使用该构建函数构建后会清空 原始数据集合-rawDataCollection 中的数据
     *
     * @param annotationExcelSourceParse 注解-数据源解析器
     * @param rawDataCollection          待导出数据集合
     */
    public SimpleAnnotationExcelConfig(AnnotationExcelSourceParse annotationExcelSourceParse, List<?> rawDataCollection) throws Exception {
        this(annotationExcelSourceParse, rawDataCollection, true);
    }


    /**
     * {@link SimpleAnnotationExcelConfig} 的唯一构造函数
     *
     * @param annotationExcelSourceParse 注解-数据源解析器
     * @param rawDataCollection          待导出数据集合
     * @param clearRowData               是否清空原始数据集合以提高效率
     */
    public SimpleAnnotationExcelConfig(AnnotationExcelSourceParse annotationExcelSourceParse,
                                       List<?> rawDataCollection, boolean clearRowData) throws Exception {
        if (ObjectUtil.isNotEmpty(rawDataCollection)) {
            this.annotationExcelSourceParse = annotationExcelSourceParse;

            // 解析待导出的原始数据
            this.excelExportDataList = annotationExcelSourceParse.parseExportDataList(rawDataCollection);

            // 处理/构造 Excel 格式处理器
            this.sourceClass = rawDataCollection.get(0).getClass();
            final List<ExportLabelSource> annotationExcelSourceList = annotationExcelSourceParse.getAnnotationExcelSourceList(sourceClass);
            this.writeHandlerList = Arrays.asList(new AnnotationHeadRowWriteHandler(rawDataCollection.size(), this),
                    new AnnotationColumnWidthWriteHandler(annotationExcelSourceList));
        } else {
            throw new TipException("导出数据不能为空");
        }

        if (clearRowData) {
            rawDataCollection.clear();
        }
    }

    /**
     * {@link SimpleAnnotationExcelConfig} 的唯一构造函数
     *
     * @param annotationExcelSourceParse 注解-数据源解析器
     * @param writeHandlerList           样式设置拦截器
     * @param rawDataCollection          待导出数据集合
     */
    public SimpleAnnotationExcelConfig(AnnotationExcelSourceParse annotationExcelSourceParse,
                                       List<WriteHandler> writeHandlerList, List<?> rawDataCollection) throws Exception {
        if (ObjectUtil.isNotEmpty(rawDataCollection)) {
            this.writeHandlerList = writeHandlerList;
            this.annotationExcelSourceParse = annotationExcelSourceParse;
            // 结息待导出的原始数据
            this.excelExportDataList = annotationExcelSourceParse.parseExportDataList(rawDataCollection);
        } else {
            throw new TipException("导出数据不能为空");
        }
    }

    @Override
    protected List<List<String>> getExcelMiddleHeaderList(int length) {
        final List<List<String>> outerCollection = new ArrayList<>(length);

        for (int index = 0; index < length; index++) {
            if (index == 0 || index == 1) {
                outerCollection.add(index, Collections.singletonList("日期：" + LocalDate.now()));
            } else if (index == length - 1 || index == length - 2) {
                outerCollection.add(index, Collections.singletonList("单位：元 / %"));
            } else {
                outerCollection.add(index, Collections.singletonList(""));
            }
        }
        return outerCollection;
    }

}
